package com.spsy.pay.order;
import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

public class WeiXinUtil {
    //订单号

    public static final String ORDER_ID = "5724200166688801129";
    public static String payOrder(String orderId) throws  Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", Constants.APP_ID);
        map.put("mch_id", Constants.MCH_ID);
        map.put("nonce_str", CommUtil.getNonce_str());
        map.put("body", "专高6-2302a-测试商品-翟亮超同学创建");
        //map.put("out_trade_no",WeiXinUtil.ORDER_ID);
        map.put("out_trade_no", orderId);
        map.put("total_fee", "1");
        map.put("spbill_create_ip", "127.0.0.1");
        map.put("notify_url", Constants.NOTIFY_URL);
        map.put("trade_type", "NATIVE");
        map.put("sign_type", "HMAC-SHA256");
        //map.put("sign",CommUtil.generateSignature(map, Constants.API_KEY, "HMACSHA256"));
        //自己写的签名算法
        map.put("sign", CommUtil2210a.generateSignature(map, Constants.API_KEY, "HMACSHA256"));
        String reqXml = XMLUtil.mapToXml(map);
        String respXml = HttpUtil.postData(Constants.UFDODER_URL, reqXml);
        System.out.println(respXml);
        Map<String, String> respMap = XMLUtil.doXMLParse(respXml);
        String codeUrl = respMap.get("code_url");
        System.out.println("------------------>....");
        System.out.println(codeUrl);
        System.out.println("------------------>....");
        return codeUrl;
    }
    public static String queryOrder(String orderId) throws Exception {
        //String orderId = WeiXinUtil.ORDER_ID;
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", Constants.APP_ID);
        map.put("mch_id", Constants.MCH_ID);
        map.put("nonce_str", CommUtil.getNonce_str());
        map.put("out_trade_no", orderId);
        map.put("trade_type", "NATIVE");
        map.put("sign_type", "HMAC-SHA256");
        //原来微信生成的sign
        //map.put("sign",CommUtil.generateSignature(map, Constants.API_KEY, "HMACSHA256"));
        //自己写的签名算法
        map.put("sign", CommUtil2210a.generateSignature(map, Constants.API_KEY, "HMACSHA256"));
        String reqXml = XMLUtil.mapToXml(map);
        String respXml = HttpUtil.postData(Constants.CHECK_URL, reqXml);
        System.out.println(respXml);
        Map<String, String> respMap = XMLUtil.doXMLParse(respXml);
        return JSON.toJSONString(respMap);
    }
    public static void main(String[] args) throws  Exception {
        WeiXinUtil.payOrder(WeiXinUtil.ORDER_ID);
//        WeiXinUtil.queryOrder(WeiXinUtil.ORDER_ID);

    }
}
